#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool isSelfCrossing(vector<int> &distance)
{
    int n = distance.size();
    if (n <= 3)
        return false;
    int step = 2;
    if (distance[step] < distance[step - 2])
    {
        while (step < n)
        {
            if (distance[step] >= distance[step - 2])
                return true;
            step++;
        } //必然要拐弯两次才能交叉
        return false;
    }
    while (step < n)
    {
        if (distance[step] <= distance[step - 2])
            break;
        step++;
    }
    // case 2
    if (step >= n)
        return false;
    // case 3
    bool flag1 = distance[step] + (step >= 4 ? distance[step - 4] : 0) >= distance[step - 2];
    bool flag2 = (step <= n - 2 ? distance[step + 1] : 0) + (step >= 3 ? distance[step - 3] : 0) >= distance[step - 1];
    if (flag2 && flag1)
        return true;
    step++;
    while (step < n)
    {
        if (distance[step] >= distance[step - 2])
            return true;
        step++;
    }
    return false;
}